package com.wyz.dao;



import java.util.List;

import my.db.QueryHelper;

import com.wyz.entity.Movie;
import com.wyz.entity.Type;


/**
 * 针对某一个实体对应表的增删改查
 * 	单个表的增删改查，和业务没有关系
 * */
public class MovieDao {
	
	/**
	 * 增加电影
	 * */
	public void save(Movie movie) {
		String sql = "insert into movie values(null,?,?,?,?,?,?,?,?,?)";
		QueryHelper.update(sql, movie.getMovieName(),movie.getReleasetime(),
				movie.getTypeId(),movie.getOverallRating(),movie.getActors(),movie.getDirector(),
				movie.getIntroduction(),movie.getReviewAmount(),movie.getMoviePicture());
	}
	/**
	 * 删除
	 * */
	public void remove(Long id) {
		String sql = "delete from movie where id=?";
		QueryHelper.update(sql, id);
	}
	
	/**
	 * 查询所有电影
	 * */
	public List<Movie> findAll() {
		String sql = "select * from movie";
		List<Movie> list = QueryHelper.query(Movie.class, sql);
		QueryHelper.load(list, "type", "typeId", Type.class);
		return list;
	}
	
	public List<Movie> findByType(Long typeId){
		String sql = "select * from movie where typeId=?";
		List<Movie> list = QueryHelper.query(Movie.class, sql,typeId);
		QueryHelper.load(list, "type", "typeId", Type.class);
		return list;
	}
	public Movie findById(Long id) {
		String sql="select * from movie where id=?";
		Movie movie = QueryHelper.queryUnique(Movie.class, sql, id);
		QueryHelper.load(movie, "type", "typeId", Type.class);
		return movie;
	}
	public static void main(String[] args) {
		
		MovieDao dao =new MovieDao();
		Movie movie =dao.findById(1L);
		System.out.println(movie.getActors());
	}
	
}
